if(!window.fly){
var fly={
		emptyFunction: function(){},abstractFunction: function(){ throw 'Unimplemented.'; },
		IS_IE: !!(window.attachEvent && !window.opera),
		IS_OPERA: !!window.opera,
		IS_GECKO: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
		IS_WEBKIT: navigator.userAgent.indexOf('AppleWebKit/') > -1,
		isString: function(v){ return typeof v == 'string'; },
		isNumber: function(v){ return typeof v == 'number'; },
		isFunction: function(v){ return typeof v == 'function'; },
		isUndefined: function(v){ return typeof v == 'undefined'; },
		isArray: function(v){ return typeof v == 'object' && v instanceof Array; },
		isArrayLike: function(v){ if( typeof v != 'object' ) return false; if( fly.isArray(v) || isFinite(v.length) ) return true; return false; },
		fn: function(v,d){ return fly.isFunction(v) ? v : fly.isString(v) ? function(){ eval(v); } : d || fly.emptyFunction; },
		$class : 'Y29tLnNoZm9yY2UuZmx5LlJlbW90ZUludm9jYXRpb25IYW5kbGVy',
		mixin: function(){
			var a = fly.array(arguments);
			if( a.length < 2 ) return a[0];
			var o,ps;
			do{ o = a.shift(); }while( !o );
			for(var i=0;i<a.length;i++) if( ( ps = a[i] ) instanceof Object ) for(var k in ps) try{ o[k] = ps[k]; }catch(e){} // ignore
																																// it.
			return o;
		},
		array: function(){
			var o,ret = [];
			for(var i=0,len=arguments.length;i<len;i++)
				if( !fly.isUndefined(o=arguments[i]) ){ if( fly.isArrayLike(o) ) for(var ii=0;ii<o.length;ii++) ret.push(o[ii]); else ret.push(o); }
			return ret;
		},
		$result:'',
		getTransport:this.IS_IE ? (function(){
			return {
				getTransport: function(){ try{ return new ActiveXObject('Msxml2.XMLHTTP'); }catch(e){ return new ActiveXObject('Microsoft.XMLHTTP'); } }
			}
		}):(function(){
			return {
				getTransport: function(){ return new XMLHttpRequest(); }
			}
		}),
		traceid:'',
		async : false,
		_genUrl :function($bean,$fn,rpctype){
			var url = '{"$rpc.name":"'+$bean+'","$class":"'+fly.$class+'","$rpc.method":"'+$fn+'","$rpc.type":"'+rpctype+'"}';
			return '.a'+Base64.encode(url)+'.json';
		},
		_invoke :function($bean,$fn,args){
			var _result='{}';
			//var url = '{"$rpc.name":"'+$bean+'","$class":"'+fly.$class+'","$rpc.method":"'+$fn+'","$rpc.type":"rpc"}';
			var param='';
			for(var i =0;i < args.length;i++){
				param += JSON.stringify(args[i])+"#";
			}
			param = param.substring(0,param.length-1);
			$.ajax({
				  type: 'POST',
				  dataType: 'JSON',
				  async:fly.async,
				  headers:{
					  'fly-invoke':'true',
					  'fly-traceid':''
				  },
				  url:fly._genUrl($bean,$fn,'rpc'),
				  data: '$rpc.args='+param,
				  statusCode:{404:function(){
				  }
				  },
				  error:function(xhr,status,error){
					  var fb = eval('('+xhr.responseText+')');
					  //fb.name fb.message fb.trace
					  alert(fb.trace);
				  },
				  success: function(result){
					   _result = result;
				  }
				});
			return _result;
		},
		_toURL:function($bean,$fn){
			//var url = '{"$rpc.name":"'+$bean+'","$class":"'+fly.$class+'","$rpc.method":"'+$fn+'","$rpc.type":"url"}';
			//return '.a'+Base64.encode(url)+'.do';
			return fly._genUrl($bean,$fn,'url');
		}
}
fly.router = function(url,params){
	var args = Array.prototype.slice.call(arguments);
	var href = args[0];
	var param='';
	for(var i = 1;i < args.length;i++){
		param += JSON.stringify(args[i])+"#";
	}
	param = param.substring(0,param.length-1);
	$.ajax({
		  type: 'POST',
		  dataType: 'JSON',
		  async:fly.async,
		  headers:{
			  'fly-invoke':'true',
			  'fly-traceid':''
		  },
		  url:href,
		  data: 'params='+param+'&$url='+href,
		  statusCode:{404:function(){
		  }
		  },
		  error:function(xhr,status,error){
			  var fb = eval('('+xhr.responseText+')');
			  //fb.name fb.message fb.trace
		  },
		  success: function(result){
			   _result = result;
		  }
		});
	return _result;
};
/**
 * methods = {'method1','method2'}
 */
fly.rpc =function (bean,methods){
	if (this == window || this == fly)
		return ;
	var _this = this,obj = {}, bind = function (f, o) {
		return function () {
			var params = _A(arguments);
			params.push(o);
			return f.apply(o, params);

	};
	}, _A = function (p) {
		var r = [], i = 0, j = p.length;
		for (; i < j; i++)
			r.push(p[i]);
		return r;
	};
	var _invoke = function(){
		var params = _A(arguments);
		var fn = params.pop();
		return fly._invoke(bean,fn,params);
	};
	var _invokeURL = function(){
		var params = _A(arguments);
		var fn = params.pop();
		return fly._toURL(bean,fn);
	};
	this.fnMakeObj = function (o, oRstObj) {
		var oT = oRstObj;
		for (var k in oT) {
			var rpc = bind(_invoke, oT[k]);
			var rpc_url = bind(_invokeURL, oT[k]);
			eval('o.'+oT[k] +'=rpc');
			eval('o.'+oT[k] +'_url=rpc_url');
		}
	};

	var isM = fly.isArray(methods);
	obj.bean = bean;
	fly.async = false;
	this.fnMakeObj(_this, methods);

}
}
/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}


